﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using TwitterClient2.Controller;
using System.Collections.ObjectModel;
using NTwitter;

namespace TwitterClient2.Components
{
    public class PanelSearch : Panel
    {
        private PanelTweetOverview panelTweetOverview;
        private Label labelSearch;
        private TextBox textBoxSearch;
        private Button buttonSearch;
        private RadioButton radioButtonFullText;
        private RadioButton radioButtonFromUser;
        private RadioButton radioButtonReplyingToUser;
        private TwitterClientController controller = TwitterClientController.getInstance();
        private PanelNavigation panelNavigation;
        private int addAtTimes = 0;
        public PanelSearch()
        {
            InitializeComponent();
        }

        private void InitializeComponent()
        {
            this.labelSearch = new System.Windows.Forms.Label();
            this.textBoxSearch = new System.Windows.Forms.TextBox();
            this.buttonSearch = new System.Windows.Forms.Button();
            this.radioButtonFullText = new System.Windows.Forms.RadioButton();
            this.radioButtonFromUser = new System.Windows.Forms.RadioButton();
            this.radioButtonReplyingToUser = new System.Windows.Forms.RadioButton();
            this.panelTweetOverview = new PanelTweetOverview(148, PanelNavigation.SEARCH_USER);
            this.panelNavigation = new PanelNavigation("Search", PanelNavigation.PANEL_SEARCH);
            this.SuspendLayout();
            // 
            // labelSearch
            // 
            this.labelSearch.Location = new System.Drawing.Point(70, 20);
            this.labelSearch.Name = "labelSearch";
            this.labelSearch.Size = new System.Drawing.Size(170, 20);
            this.labelSearch.Text = "What do you are looking for?";
            // 
            // textBoxSearch
            // 
            this.textBoxSearch.Location = new System.Drawing.Point(10, 40);
            this.textBoxSearch.Name = "textBoxSearch";
            this.textBoxSearch.Size = new System.Drawing.Size(220, 21);
            this.textBoxSearch.TabIndex = 0;
            // 
            // buttonSearch
            // 
            this.buttonSearch.Location = new System.Drawing.Point(158, 63);
            this.buttonSearch.Name = "buttonSearch";
            this.buttonSearch.Size = new System.Drawing.Size(72, 20);
            this.buttonSearch.TabIndex = 0;
            this.buttonSearch.Text = "search";
            this.buttonSearch.Click += new System.EventHandler(this.buttonSearch_Click);
            // 
            // radioButtonFullText
            // 
            this.radioButtonFullText.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
            this.radioButtonFullText.Location = new System.Drawing.Point(10, 87);
            this.radioButtonFullText.Name = "radioButtonFullText";
            this.radioButtonFullText.Size = new System.Drawing.Size(73, 16);
            this.radioButtonFullText.TabIndex = 0;
            this.radioButtonFullText.Text = "full text";
            this.radioButtonFullText.Checked = true;
            this.radioButtonFullText.CheckedChanged += new System.EventHandler(this.radioButtonFullText_CheckedChanged);
            // 
            // radioButtonFromUser
            // 
            this.radioButtonFromUser.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
            this.radioButtonFromUser.Location = new System.Drawing.Point(83, 87);
            this.radioButtonFromUser.Name = "radioButtonFromUser";
            this.radioButtonFromUser.Size = new System.Drawing.Size(73, 16);
            this.radioButtonFromUser.TabIndex = 0;
            this.radioButtonFromUser.Text = "from user";
            this.radioButtonFromUser.CheckedChanged += new System.EventHandler(this.radioButtonFromUser_CheckedChanged);
            // 
            // radioButtonReplyingToUser
            // 
            this.radioButtonReplyingToUser.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
            this.radioButtonReplyingToUser.Location = new System.Drawing.Point(156, 87);
            this.radioButtonReplyingToUser.Name = "radioButtonReplyingToUser";
            this.radioButtonReplyingToUser.Size = new System.Drawing.Size(73, 16);
            this.radioButtonReplyingToUser.TabIndex = 0;
            this.radioButtonReplyingToUser.Text = "replying to user";
            this.radioButtonReplyingToUser.CheckedChanged += new System.EventHandler(this.radioButtonReplyingToUser_CheckedChanged);
            // 
            // panelNavigation
            // 
            this.panelNavigation.Location = new System.Drawing.Point(10, 0);
            this.panelNavigation.Name = "panelNavigation";

            // 
            // panelTweetOverview
            // 
            this.panelTweetOverview.Location = new System.Drawing.Point(10, 110);
            this.panelTweetOverview.Name = "panelTweetOverview";

            // 
            // PanelSearch
            // 
            this.Controls.Add(this.labelSearch);
            this.Controls.Add(this.textBoxSearch);
            this.Controls.Add(this.buttonSearch);
            this.Controls.Add(this.radioButtonFromUser);
            this.Controls.Add(this.radioButtonFullText);
            this.Controls.Add(this.radioButtonReplyingToUser);
            this.Controls.Add(this.panelNavigation);
            this.Controls.Add(this.panelTweetOverview);
            this.ResumeLayout(false);

        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            string searchRequest = textBoxSearch.Text;
            if (!searchRequest.Equals(""))
            {
                TwitterClientEvent tEvent = new TwitterClientEvent();

                if (this.radioButtonFullText.Checked)
                {
                    tEvent.paramList.Add(1);
                    tEvent.paramList.Add(searchRequest);
                }
                else if (this.radioButtonFromUser.Checked)
                {
                    tEvent.paramList.Add(2);
                    tEvent.paramList.Add(searchRequest.Substring(1, searchRequest.Length - 1));
                }
                else
                {
                    tEvent.paramList.Add(3);
                    tEvent.paramList.Add(searchRequest.Substring(1, searchRequest.Length - 1));
                }

                Cursor.Current = Cursors.WaitCursor;
                this.controller.performUserInput(TwitterClientController.SEARCH_BUTTON_CLICK, tEvent);
                Cursor.Current = Cursors.Default;
            }

        }

        public void UpdateData(Collection<Status> c)
        {
            this.panelTweetOverview.UpdateData(c);
        }

        private void radioButtonFullText_CheckedChanged(object sender, EventArgs e)
        {
            string text = this.textBoxSearch.Text;
            if (text.Contains("@"))
            {
                this.textBoxSearch.Text = text.Substring(1, text.Length - 1);
                this.addAtTimes = 0;
            }
        }

        private void radioButtonFromUser_CheckedChanged(object sender, EventArgs e)
        {
            if (this.addAtTimes == 0)
            {
                string text = "@" + this.textBoxSearch.Text;
                this.textBoxSearch.Text = text;
                this.addAtTimes = 1;
            }
        }

        private void radioButtonReplyingToUser_CheckedChanged(object sender, EventArgs e)
        {
            if (this.addAtTimes == 0)
            {
                string text = "@" + this.textBoxSearch.Text;
                this.textBoxSearch.Text = text;
                this.addAtTimes = 1;
            }
        }
    }
}
